﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;

public partial class UI_DBbackup : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        FTchina.basic.CheckAdmin(this);
        FTchina.basic.ChkPoewr(this, FTchina.Api.htmlpath.GetCurrentPagePath());

        if (!IsPostBack)
        {
            string act = FTchina.basic.FilterSQL(Request["act"]);
            switch (act)
            {

                case "del":
                    {
                        DeleteData();
                        break;
                    }
                case "back":
                    {
                        RestoreData();
                        break;
                    }

            }
            bindData();

        }
    }

    /// <summary>
    /// 还原
    /// </summary>
    private void RestoreData()
    {
        string file = FTchina.basic.FilterSQL(Request["path"]);
        if (file == "db.db") return;

        File.Copy(Server.MapPath("~/app_data/" + file), Server.MapPath("~/app_data/db.db"), true);
        FTchina.UImsg.PageRender(true);


    }

    /// <summary>
    /// 删除
    /// </summary>
    private void DeleteData()
    {
        string file = FTchina.basic.FilterSQL(Request["path"]);
        if (file == "db.db") return;
        string filePath = Server.MapPath("~/app_data/" + file);

        if (File.Exists(filePath))
        {
            File.Delete(filePath);
        }
        FTchina.UImsg.PageRender(true);

    }


    protected DataTable FileInfo2DataTable(FileInfo[] fi)
    {
        Array.Sort<FileInfo>(fi, new FIleLastTimeComparer());

        string nowpath = Server.MapPath(".").ToString();
        DataTable dt = new DataTable();
        dt.Columns.Add("name", typeof(string));
        dt.Columns.Add("Cpath", typeof(string));
        dt.Columns.Add("Cdate", typeof(string));
        dt.Columns.Add("Csize", typeof(string));


        foreach (FileInfo f in fi)
        {
            if (f.Name == "db.db") continue;
            DataRow newRow = dt.NewRow();
            newRow["name"] = f.Name.ToString();
            newRow["Cpath"] = f.DirectoryName.ToString().Replace(nowpath, "");
            newRow["Cdate"] = f.CreationTime.ToString();
            newRow["Csize"] = (f.Length / 1024).ToString() + "KB";
            dt.Rows.Add(newRow);
        }
        return dt;
    }




    void bindData()
    {
        DirectoryInfo imagesfile = new DirectoryInfo(Server.MapPath("~/app_data/"));

        DataTable dt = FileInfo2DataTable(imagesfile.GetFiles("*.db"));

        //gvList.DataSource = imagesfile.GetFiles("*.jpg");
        gvList.DataSource = dt;
        gvList.DataBind();


        gvList.DataBind();
        LblCurrentIndex.Text = (gvList.PageIndex + 1).ToString();
        LblPageCount.Text = gvList.PageCount.ToString();
        LblRecordCount.Text = dt.Rows.Count.ToString();
        if (dt.Rows.Count == 0)
        {
            btnFirst.Visible = false;
            btnPrev.Visible = false;
            btnNext.Visible = false;
            btnLast.Visible = false;

            LblCurrentIndex.Visible = false;
            LblPageCount.Visible = false;
            LblRecordCount.Visible = false;
            Label1.Visible = false;
            Label1.Visible = false;
            Label2.Visible = false;
            Label3.Visible = false;
            Label4.Visible = false;
            Label5.Visible = false;
            Label6.Visible = false;

            //lbMessage.Text = "没有记录！";
        }
        else if (gvList.PageCount == 1)
        {
            btnFirst.Visible = false;
            btnPrev.Visible = false;
            btnNext.Visible = false;
            btnLast.Visible = false;
        }
        else
        {
            btnFirst.Visible = true;
            btnPrev.Visible = true;
            btnNext.Visible = true;
            btnLast.Visible = true;
            LblCurrentIndex.Visible = true;
            LblPageCount.Visible = true;
            LblRecordCount.Visible = true;
            Label1.Visible = true;
            Label1.Visible = true;
            Label2.Visible = true;
            Label3.Visible = true;
            Label4.Visible = true;
            Label5.Visible = true;
            Label6.Visible = true;
        }
        // 计算生成分页页码,分别为："首 页" "上一页" "下一页" "尾 页"
        btnFirst.CommandName = "1";
        btnPrev.CommandName = (gvList.PageIndex == 0 ? "1" : gvList.PageIndex.ToString());
        btnNext.CommandName = (gvList.PageCount == 1 ? gvList.PageCount.ToString() : (gvList.PageIndex + 2).ToString());
        btnLast.CommandName = gvList.PageCount.ToString();

    }
    protected void PagerButtonClick(object sender, EventArgs e)
    {
        gvList.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
        bindData();
    }
    protected void gvList_PageIndexChanging(Object sender, GridViewPageEventArgs e)
    {
        if (gvList.PageCount > 1)
        {
            gvList.PageIndex = e.NewPageIndex;
            bindData();
        }
        else
        {
            LblCurrentIndex.Text = gvList.EditIndex.ToString();
        }
    }


    protected void btnbackup_Click(object sender, EventArgs e)
    {

        File.Copy(Server.MapPath("~/app_data/db.db"), Server.MapPath("~/app_data/db" + DateTime.Now.ToString("yyyyMMddhhmmss") + DateTime.Now.Ticks.ToString().Substring(0, 3) + ".db"), true);

        FTchina.UImsg.PageRender(true);

    }



    //文件夹中按时间排序最新的文件读取   

    public class DirectoryLastTimeComparer : IComparer<DirectoryInfo>
    {

        #region IComparer<DirectoryInfo> 成员
        public int Compare(DirectoryInfo x, DirectoryInfo y)
        {

            return x.LastWriteTime.CompareTo(y.LastWriteTime);
        }

        #endregion

    }
    //文件夹中按时间排序最新的文件读取   

    public class FIleLastTimeComparer : IComparer<FileInfo>
    {

        #region IComparer<FileInfo> 成员
        public int Compare(FileInfo y, FileInfo x)
        {
            return x.CreationTime.CompareTo(y.CreationTime);
        }

        #endregion

    }
}